package com.zhiyou.aifeng.module_login

import android.os.Bundle
import android.view.View
import cn.jpush.android.api.JPushInterface
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.zhiyou.aifeng.common.base.oneClick
import com.zhiyou.aifeng.common.helper.*
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.router.Routers
import com.zhiyou.aifeng.common_ui.utils.bindLifeCycle
import com.zhiyou.aifeng.common_ui.utils.showAreaDialog
import com.zhiyou.aifeng.common_ui.utils.subscribeByNoError
import com.zhiyou.aifeng.viewmodel.LoginViewModel
import kotlinx.android.synthetic.main.login_activity_apply.*
import org.jetbrains.anko.longToast
import org.jetbrains.anko.toast

@Route(path = Routers.apply)
class ApplyActivity : BaseActivity() {

    private var mProvinceId = ""
    private var mCityId = ""
    private var mDistrictId = ""
    private var mAdress = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_activity_apply)
        initTitle(title= "申请加入东方红车队", name = "说明", divider = false)
    }

    override fun getViewModel() = get<LoginViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        tvRight?.oneClick {
            ARouter.getInstance()
                .build(Routers.deal)
                .withString("title", "申请使用说明")
                .navigation()
        }
        apply_area.oneClick {
            getViewModel().getAreaList(this)
                .bindLifeCycle(getLifecycleOwner())
                .subscribe {
                    showAreaDialog(
                        items = it,
                        onResult = { ids, adress ->
                            mProvinceId = ids[0]
                            mCityId = ids[1]
                            mDistrictId = ids[2]
                            mAdress = adress
                            apply_area.setRightString(mAdress)
                        }
                    )
                }
        }
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_apply -> {
                when {
                    apply_name.text.isBlank() -> {
                        apply_name.requestFocus()
                        toast("请输入姓名")
                        return
                    }
                    apply_tel.text.isBlank() -> {
                        apply_tel.requestFocus()
                        toast("请输入手机号")
                        return
                    }
                    apply_id.text.isBlank() -> {
                        apply_id.requestFocus()
                        toast("请输入身份证号码")
                        return
                    }
                    mAdress.isEmpty() -> {
                        toast("请选择所在地区")
                        return
                    }
                    !apply_tel.text.isMobile() -> {
                        apply_tel.requestFocus()
                        toast("请输入正确的手机号")
                        return
                    }
                    !CommonUtil.IDCardValidate(apply_id.textString()) -> {
                        apply_id.requestFocus()
                        toast("请输入正确的身份证号码")
                        return
                    }
                }

                getViewModel().apply(
                    "name" to apply_name.trimString(),
                    "phone" to apply_tel.textString(),
                    "cardid" to apply_id.textString(),
                    "province" to mProvinceId,
                    "city" to mCityId,
                    "district" to mDistrictId,
                    "regionAddr" to mAdress,
                    "jpushid" to JPushInterface.getRegistrationID(baseContext)
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            longToast("您的申请已提交，请耐心等待！")
                            finishView()
                        }
                    )
            }
        }
    }

}